package com.jerry.springcloud.controller;

import com.jerry.springcloud.entity.CommonResult;
import com.jerry.springcloud.entity.Payment;
import com.jerry.springcloud.service.PaymentFeignService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@Slf4j
public class PaymentController {

    @Resource
    private PaymentFeignService paymentFeignService;

    @GetMapping("/consumer/payment/create")
    public CommonResult create(Payment payment) {
        return paymentFeignService.create(payment);
    }

    @GetMapping("/consumer/payment/get/{id}")
    public CommonResult getPaymentById(@PathVariable("id") Long id) {
        return paymentFeignService.getPaymentById(id);
    }

    @GetMapping("/consumer/payment/get")
    public CommonResult getPaymentList() {
        return paymentFeignService.getPaymentList();
    }

    @GetMapping("/consumer/payment/hystrix/ok/{id}")
    String paymentInfo_OK(@PathVariable("id") Long id) {
        return paymentFeignService.paymentInfo_OK(id);
    }

    @GetMapping("/consumer/payment/hystrix/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentInfo_Timeout_Handler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000")
    })
    String paymentInfo_Timeout(@PathVariable("id") Long id) {
        return paymentFeignService.paymentInfo_Timeout(id);
    }

    @GetMapping("/consumer/payment/hystrix/error/{id}")
    String paymentInfo_Error(@PathVariable("id") Long id) {
        return paymentFeignService.paymentInfo_Error(id);
    }

    @GetMapping("/consumer/payment/hystrix/circuitBreaker/{id}")
    String paymentInfo_CircuitBreaker(@PathVariable("id") Long id) {
        return paymentFeignService.paymentInfo_CircuitBreaker(id);
    }

    public String paymentInfo_Timeout_Handler(Long id) {
        StringBuffer sb = new StringBuffer();
        sb.append("消费者 ****** paymentInfo_Timeout_Handler ****** ");
        sb.append("线程池=== "+Thread.currentThread().getName() +"******");
        sb.append("id==="+id);
        return sb.toString();
    }

    @GetMapping("/consumer/payment/zipkin")
    String paymentInfo_zipkin() {
        return paymentFeignService.paymentInfo_zipkin();
    }

}
